//
//  TeaterRegistViewController.m
//  ZhiBoYuan
//
//  Created by apple on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TeaterRegistViewController.h"
#import "YJSelectionView.h"
#import "WTTableAlertView.h"

@interface TeaterRegistViewController ()
<
UITextFieldDelegate
>
@property (weak, nonatomic) IBOutlet UITextField *Afild;
@property (weak, nonatomic) IBOutlet UIButton *Asender;
@property (weak, nonatomic) IBOutlet UITextField *Bfild;
@property (nonatomic, strong)UIButton *bButton;
@property (weak, nonatomic) IBOutlet UITextField *Cfild;
@property (weak, nonatomic) IBOutlet UITextField *Dfield;
@property (weak, nonatomic) IBOutlet UITextField *Efield;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *highntFild;

//@property (nonatomic,strong)UIPickerView *pickerView;

@property (nonatomic,strong)NSArray *nameArray;

@property (nonatomic, strong)UIButton *commiteBtn;

@property (nonatomic, strong)NSArray *nianjiArray;

@property (nonatomic, strong)NSArray *nianJiNameArray;

@property (nonatomic, strong)NSMutableArray *xuekeArray;

@property (nonatomic, strong)NSMutableArray *xuekeNameArray;

@property (nonatomic, assign)int xuekeId;
@property (nonatomic, assign)int nianjiId;

@end

@implementation TeaterRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"教师资格认证";
    [self loadXueKeData];
    [self loadNianJiData];
    
    _highntFild.constant = HeightConfig(40);
    
    
    CGFloat w = 100;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, w, 40)];
    label.text = @"年级";
    _Afild.leftView= label;
    [_Afild setLeftViewMode:(UITextFieldViewModeAlways)];

    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, w, 40)];
    label2.text = @"学科";
    _Bfild.leftView= label2;
    [_Bfild setLeftViewMode:(UITextFieldViewModeAlways)];
    
    _bButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _bButton.frame = CGRectMake(0, 0, w + 200, 40);
    _bButton.backgroundColor = [UIColor clearColor];
    [_bButton addTarget:self action:@selector(BTouch) forControlEvents:(UIControlEventTouchUpInside)];
    [_Bfild addSubview:_bButton];
    
    
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, w, 40)];
    label3.text = @"职称";
    _Cfild.leftView= label3;
    [_Cfild setLeftViewMode:(UITextFieldViewModeAlways)];
    
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, w, 40)];
    label4.text = @"教师认证码";
    _Dfield.leftView= label4;
    [_Dfield setLeftViewMode:(UITextFieldViewModeAlways)];
    
    UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, w, 40)];
    label5.text = @"简介";
    _Efield.leftView= label5;
    [_Efield setLeftViewMode:(UITextFieldViewModeAlways)];
    
    BTN_MSG_FUNC(_Asender, Atouch);
    
    
    _commiteBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _commiteBtn.frame = CGRectMake(20 * KscreenWidth, Screenheight - 200 * KscreenHeight, ScreenWidth - 40 * KscreenWidth, 50 * KscreenHeight);
    [_commiteBtn setTitle:@"提交认证" forState:0];
    _commiteBtn.backgroundColor = ZhuTiColor;
    [_commiteBtn addTarget:self action:@selector(commiteAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_commiteBtn];
}

-(void)loadXueKeData{
    
    _xuekeArray = [NSMutableArray array];
    _xuekeNameArray = [NSMutableArray array];
    
       SGParameter *parameter  = [[SGParameter alloc] init];
        parameter.body = @{
                           };
        [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Course&a=xuekeList",HTTPP] parameter:parameter sucess:^(id responseObject) {
            NSLog(@"%@",responseObject);
    
            NSDictionary *dic = responseObject;
    
            if ([dic[@"status"] intValue] == 200) {
                for (NSDictionary *dicc in dic[@"data"][@"xueke_list"]) {
                    [_xuekeArray addObject:dicc];
                    [_xuekeNameArray addObject:[dicc valueForKey:@"name"]];
                }
                
            }else{
                [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:3.0f];
            }
            NSLog(@"%lu",(unsigned long)_xuekeNameArray.count);
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
    
        }];
    
}

-(void)loadNianJiData{
//    _nianjiArray = [NSMutableArray array];
//    _nianJiNameArray = [NSMutableArray array];
    
    _nianJiNameArray = @[@"一年级",@"二年级",@"三年级",@"四年级",@"五年级",@"六年级",@"七年级",@"八年级",@"九年级",@"十年级",@"十一年级",@"十二年级"];
    
    _nianjiArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
    
//    SGParameter *parameter  = [[SGParameter alloc] init];
//    parameter.body = @{
//                       };
//
//    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Teacher&a=getGradeList",HTTPP] parameter:parameter sucess:^(id responseObject) {
//        NSLog(@"%@",responseObject);
//
//        NSDictionary *dic = responseObject;
//
//            if ([dic[@"status"] intValue] == 200) {
//                for (NSDictionary *dicc in dic[@"data"][@"grade_list"]) {
//                    [_nianjiArray addObject:dicc];
//                    [_nianJiNameArray addObject:[dicc valueForKey:@"grade_name"]];
//                }
//
//        }else{
//            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:3.0f];
//        }
//
//
//
//
//    } failure:^(NSError *error) {
//        NSLog(@"%@",error);
//
//    }];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_Afild resignFirstResponder];
    [_Asender resignFirstResponder];
    [_Bfild resignFirstResponder];
    [_Cfild resignFirstResponder];
    [_Dfield resignFirstResponder];
    [_Efield resignFirstResponder];
}

-(void)commiteAction{
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"xueke_id":[_xuekeArray[_xuekeId] valueForKey:@"id"],
                       @"greade_id":[NSString stringWithFormat:@"%d",_nianjiId + 1],
                       @"level_name":_Cfild.text,
                       @"invite_teacher_code":_Dfield.text,
                       @"teacher_detail":_Efield.text
                       };

    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Teacher&a=teacherCeitify",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);

        NSDictionary *dic = responseObject;
        
        if ([dic[@"status"] intValue] == 200) {
            
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"认证审核中，请耐心等待，审核通过后方可登陆" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil, nil];
            [alertView show];
            
            UIViewController *viewCtl = self.navigationController.viewControllers[0];
            [self.navigationController popToViewController:viewCtl animated:YES];

        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:3.0f];
        }




    } failure:^(NSError *error) {
        NSLog(@"%@",error);

    }];
    
}

-(void)Atouch{
    WTTableAlertView* alertview = [WTTableAlertView initWithTitle:@"" options:_nianJiNameArray singleSelection:YES selectedItems:@[@(0)] completionHandler:^(NSArray * _Nullable options) {
    
        for (id obj in options) {
            
            int aa = [obj intValue];
            _nianjiId = aa;
            _Afild.text = [NSString stringWithFormat:@"%@",_nianJiNameArray[aa]];
        }
        
    }];
    alertview.hiddenConfirBtn = YES;
    [alertview show];
    
}

-(void)BTouch{
    
    WTTableAlertView* alertview = [WTTableAlertView initWithTitle:@"" options:_xuekeNameArray singleSelection:YES selectedItems:@[@(0)] completionHandler:^(NSArray * _Nullable options) {
        
        for (id obj in options) {
            
            int aa = [obj intValue];
            _xuekeId = aa;
            _Bfild.text = [NSString stringWithFormat:@"%@",_xuekeNameArray[aa]];
        }
        
    }];
    alertview.hiddenConfirBtn = YES;
    [alertview show];
    
}
//-(void)textFieldDidBeginEditing:(UITextField *)textField{
//    if ( textField == _Afild ) {
//
//         NSLog(@"11111111111111");
//    }else{
//        NSLog(@"22222\n");
//    }
//}

//- (BOOL) textFieldShouldBeginEditing:(UITextField*) textField
//{
//    NSLog(@"文本将要编辑............%zd",textField.tag);
//
//    if ( textField.tag == 101  ) {
//        NSLog(@"11111111111111");
//        [textField resignFirstResponder];
//
//        NSArray *arr =@[@"一年假", @"二年级", @"三年级",@"四年级",@"五年级",@"六年级",@"七年级",@"八年级",@"九年级"];
//        [YJSelectionView showWithTitle:@"请选择年级" options:arr singleSelection:YES delegate:self completionHandler:^(NSInteger index, NSArray *array) {
//            NSLog(@"%ld", index);
//
//            if (index <=  -1)
//            {
//                NSLog(@"取消");
//            }
//            else
//            {
//                _Afild.text = arr[index];
//
//            }
//        }];
//
//         return NO;
//    }else{
//        NSLog(@"22222\n");
//         return YES;
//    }
//
//}


- (void)buttonSender:(UIButton *)sender{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
